import * as Koa from 'koa';
import { addLoginLog, delLoginLog, updateLoginLog, searchLoginLog } from "../entity/login-log";
import { joinDefaultFields } from "../utils/index";
const dayjs = require("dayjs");
// TODO：commonjs 模式下引用 esmodule 模块
let nanoid: any = null;
(async () => {
  nanoid = (await eval("import('nanoid')")).nanoid;
})();

const errorMsg = `服务器错误：${'loginLog'}`;
const successMsg = `成功：${'loginLog'}`;

export default class LoginLogService {
  /**
   * 添加
   * @param ctx 
   */
  addLoginLog = async (ctx: Koa.Context) => {
    try {
      const loginLog = ctx.request.body;
      // 新增
      const _loginLog = joinDefaultFields(loginLog, "loginLogFields");
      _loginLog.llid = nanoid(18);
      _loginLog.login_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
      const result: any = _loginLog && await addLoginLog(ctx, _loginLog);
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

    /**
   * 删除
   * @param ctx 
   */
  delLoginLog = async (ctx: Koa.Context) => {
    try {
      const { llid } = ctx.request.body;
      if (!llid || !llid.length) return ctx.fail(null, "llid不能为空", -1);
      const llids = Array.isArray(llid)? llid : [llid];
      let condition = `llid IN (${llids.map((i) => `"${i}"`).join(",")})`;
      const result: any =  !!llids.length && await delLoginLog(ctx, { condition });
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

    /**
   * 修改
   * @param ctx 
   */
  updateLoginLog = async (ctx: Koa.Context) => {
    try {
      const loginLog = ctx.request.body;
      loginLog.update_time = dayjs().format("YYYY-MM-DD HH:mm:ss"); // 最后的更新时间
      const result: any = loginLog && await updateLoginLog(ctx, loginLog);
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(null, successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };

  /**
   * 查询
   * @param ctx 
   */
  searchLoginLog = async (ctx: Koa.Context) => {
    try {
      const params = ctx.request.body;
      const { llid, uid, login_status } = params || {}; // 查询条件
      // 相等的条件
      let condition = "";
      let equals: string | string[] = [];
      llid && (equals.push(` llid = "${llid}"`));
      uid && (equals.push(` uid = "${uid}"`));
      login_status && (equals.push(` login_status = "${login_status}"`));
      equals = equals.join(" AND");
      equals && (condition += `WHERE ${equals}`);
      const result: any = await searchLoginLog(ctx, { condition });
      if (!result) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else if (!result.success) {
        ctx.fail(null, result.msg || errorMsg, -1);
      } else {
        ctx.success(result.data || [], successMsg);
      }
    } catch (error) {
      ctx.fail(null, errorMsg, -1);
    }
  };
}
